package Controller;

import Utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by asd73 on 2017/8/23.
 */
@Controller
/*@RequestMapping(value = "/test")*/
public class FileUploadDown extends HttpServlet{

    /**
     * 文件上传
     * @param multipartFile
     * @param fileName
     * @return
     */
    @RequestMapping(value = "/fileupload")
    @ResponseBody
    public ResultUtils upload(MultipartFile multipartFile, String fileName){
        String filePath = PathUtil.getClasspath() + "uploadFiles/uploadImgs/";
        String result = FileUpload.fileUp(multipartFile,filePath,fileName);
        return ResultUtils.ok(filePath+result);
    }

    /**
     * 文件下载
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/download")
    @ResponseBody
    public ResultUtils download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        byte[] data = FileUtil.toByteArray2(PathUtil.getClasspath()+"uploadFiles/uploadImgs/testImage.docx");
        String filePath = PathUtil.getClasspath() + "uploadFiles/uploadImgs/testImage.docx";
        try {
            FileDownload.fileDownload(response,filePath,"test.docx");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.ok();
    }
}
